﻿Imports Control_Vuelos_BL
Imports Control_Vuelos_BE
Imports System.Data.SqlClient

Public Class frmTabla_MEmpresa
    Inherits frmBaseMantenimiento

    Private _M_EmpresaTransporte As New M_EmpresaTransporteBL
    Private _M_EmpresaTransporteBE As New M_EmpresaTransporteBE
    Private opcion As Integer = 0
    Private _Analisis As New AnalisisBL

    Private Sub frmTabla_MEmpresa_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        SG_Control_Vuelos_Main.RibbonPageGroup1.Visible = True
        SG_Control_Vuelos_Main.RibbonPageGroup2.Visible = True
        SG_Control_Vuelos_Main.RibbonPageGroup3.Visible = True
        SG_Control_Vuelos_Main.RibbonPageGroup4.Visible = True
        SG_Control_Vuelos_Main.RibbonPageGroup5.Visible = True
        SG_Control_Vuelos_Main.RibbonPageGroup6.Visible = True
        SG_Control_Vuelos_Main.RibbonPageGroup7.Visible = True
        SG_Control_Vuelos_Main.RibbonPageGroup9.Visible = True
    End Sub

    Private Sub frmTabla_MEmpresa_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        AddHandler BarBtnAgregar.ItemClick, AddressOf pr_RegistroNuevo
        AddHandler BarBtnModificar.ItemClick, AddressOf pr_Modificar
        AddHandler BarBtnEliminar.ItemClick, AddressOf pr_Eliminar
        AddHandler BarBtnGrabar.ItemClick, AddressOf pr_Grabar
        AddHandler BarBtnCancelar.ItemClick, AddressOf pr_Cancelar
        AddHandler BarBtnRefresh.ItemClick, AddressOf pr_Refresh
        AddHandler BarBtnImprimir.ItemClick, AddressOf pr_Imprimir
        ModM_EmpresaTransporte1.cbCodAnalisis.Properties.DataSource = _Analisis.ListarAnalisis("")
        ModM_EmpresaTransporte1.cbCodAnalisis.Properties.DisplayMember = "Desanl"
        ModM_EmpresaTransporte1.cbCodAnalisis.Properties.ValueMember = "Codanl"
        pr_Refresh()
        InicioBtn()
    End Sub

    Private Sub pr_Imprimir()
        Dim RptLugar As New XRptM_EmpresaTransporte()
        RptLugar.DataSource = GridControl1.DataSource
        RptLugar.DataMember = "M_EmpresaTransporte"
        RptLugar.ShowRibbonPreviewDialog()
    End Sub

    Private Sub pr_Refresh()
        GridControl1.DataSource = _M_EmpresaTransporte.ListarM_EmpresaTransporte("")
    End Sub

    Private Sub InicioBtn()
        BarBtnAgregar.Enabled = True
        BarBtnModificar.Enabled = True
        BarBtnEliminar.Enabled = True
        BarBtnGrabar.Enabled = False
        BarBtnCancelar.Enabled = False
        BarBtnRefresh.Enabled = True
    End Sub

    Private Sub ProcessBtn()
        BarBtnAgregar.Enabled = False
        BarBtnModificar.Enabled = False
        BarBtnEliminar.Enabled = False
        BarBtnGrabar.Enabled = True
        BarBtnCancelar.Enabled = True
        BarBtnRefresh.Enabled = False
    End Sub

    Private Sub pr_Eliminar()
        If GridView1.RowCount > 0 Then
            Dim row As DataRow = TryCast(GridView1.GetDataRow(GridView1.GetSelectedRows()(0)), DataRow)
            If Not row Is Nothing Then
                If MsgBox("¿Está seguro de eliminar el registro?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Desición") = MsgBoxResult.Yes Then
                    _M_EmpresaTransporteBE.CodEmpresa = ModM_EmpresaTransporte1.gCodEmpresa.EditValue
                    If _M_EmpresaTransporte.pr_EliminarM_EmpresaTransporte(_M_EmpresaTransporteBE) Then
                        pr_Refresh()
                        FocusedFila()
                    End If
                End If
            End If
        End If
        opcion = 0
    End Sub

    Private Sub pr_Modificar()
        If GridView1.RowCount > 0 Then
            Dim row As DataRow = TryCast(GridView1.GetDataRow(GridView1.GetSelectedRows()(0)), DataRow)
            If Not row Is Nothing Then
                XtraTabPage1.Text = " General (EDITAR) "
                ModM_EmpresaTransporte1.gCodEmpresa.EditValue = row("CodEmpresa")
                ModM_EmpresaTransporte1.gCodEmpresa.Properties.ReadOnly = True
                ModM_EmpresaTransporte1.gNomEmpresa.EditValue = row("NomEmpresa")
                ModM_EmpresaTransporte1.gNomEmpresa.Properties.ReadOnly = False
                ModM_EmpresaTransporte1.cbCodAnalisis.EditValue = row("CodAnalisis")
                ModM_EmpresaTransporte1.cbCodAnalisis.Properties.ReadOnly = False
                ModM_EmpresaTransporte1.gNomEmpresa.Focus()
                GridControl1.Enabled = False
                opcion = 2
            End If
        End If
        ProcessBtn()
    End Sub

    Private Sub pr_Cancelar()
        GridControl1.Enabled = True
        GridControl1.Focus()
        FocusedFila()
        opcion = 0
        XtraTabPage1.Text = " General "
        InicioBtn()
    End Sub

    Private Sub pr_Grabar()
        If ModM_EmpresaTransporte1.DxValidationProvider1.Validate Then
            XtraTabPage1.Text = " General "
            GridControl1.Enabled = True
            GridControl1.Focus()
            _M_EmpresaTransporteBE.CodEmpresa = ModM_EmpresaTransporte1.gCodEmpresa.EditValue
            _M_EmpresaTransporteBE.NomEmpresa = ModM_EmpresaTransporte1.gNomEmpresa.EditValue
            _M_EmpresaTransporteBE.CodAnalisis = ModM_EmpresaTransporte1.cbCodAnalisis.EditValue
            If opcion = 1 Then
                If _M_EmpresaTransporte.pr_AgregarM_EmpresaTransporte(_M_EmpresaTransporteBE) Then
                    pr_Refresh()
                End If
            End If
            If opcion = 2 Then
                If _M_EmpresaTransporte.pr_ModificarM_EmpresaTransporte(_M_EmpresaTransporteBE) Then
                    pr_Refresh()
                End If
            End If
        End If
        GridControl1.Enabled = True
        GridControl1.Focus()
        GridControl1.Select()
        opcion = 0
        InicioBtn()
    End Sub

    Private Sub pr_RegistroNuevo()
        XtraTabPage1.Text = " General (NUEVO) "
        ModM_EmpresaTransporte1.gCodEmpresa.EditValue = ""
        ModM_EmpresaTransporte1.gCodEmpresa.Properties.ReadOnly = False
        ModM_EmpresaTransporte1.gNomEmpresa.EditValue = ""
        ModM_EmpresaTransporte1.gNomEmpresa.Properties.ReadOnly = False
        ModM_EmpresaTransporte1.cbCodAnalisis.EditValue = ""
        ModM_EmpresaTransporte1.cbCodAnalisis.Properties.ReadOnly = False
        ModM_EmpresaTransporte1.gCodEmpresa.Focus()
        GridControl1.Enabled = False
        opcion = 1
        ProcessBtn()
    End Sub

    Private Sub GridView1_FocusedRowChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles GridView1.FocusedRowChanged
        If GridView1.RowCount > 0 Then
            FocusedFila()
        End If
    End Sub

    Private Sub FocusedFila()
        If GridView1.RowCount > 0 Then
            Dim row As DataRow = TryCast(GridView1.GetDataRow(GridView1.GetSelectedRows()(0)), DataRow)
            If Not row Is Nothing Then
                ModM_EmpresaTransporte1.gCodEmpresa.EditValue = row("CodEmpresa")
                ModM_EmpresaTransporte1.gCodEmpresa.Properties.ReadOnly = True
                ModM_EmpresaTransporte1.gNomEmpresa.EditValue = row("NomEmpresa")
                ModM_EmpresaTransporte1.gNomEmpresa.Properties.ReadOnly = True
                ModM_EmpresaTransporte1.cbCodAnalisis.EditValue = row("CodAnalisis")
                ModM_EmpresaTransporte1.cbCodAnalisis.Properties.ReadOnly = True
            End If
        End If
    End Sub

End Class